package com.cxh.rmsq.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.VideoView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.cxh.rmsq.R
import com.cxh.rmsq.utils.MyUtils

class MediaPagerAdapter(
    val mediaList: List</*PreViewImgOrVideoActivity.MediaItem*/String>,
    private val context: Context, val type: Int
) : RecyclerView.Adapter<MediaPagerAdapter.MediaViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return MediaViewHolder(inflater.inflate(R.layout.item_media, parent, false))
    }

    override fun onBindViewHolder(holder: MediaViewHolder, position: Int) {
        if (type == 1) {
            holder.imageView.isVisible = true
            holder.videoView.isVisible = false
            loadImage(holder.imageView, mediaList[position])
        } else if (type == 2) {
            holder.imageView.isVisible = false
            holder.videoView.isVisible = true
            MyUtils.playVideoFromFile(context, mediaList[position], holder.videoView)
        }
//        val item = mediaList[position]
//        when (item.type) {
//            1 -> loadImage(holder.imageView, item.url.toString())
////            PreViewImgOrVideoActivity.MediaItem.MediaType.IMAGE -> loadImage(holder.imageView, item.url)
////            PreViewImgOrVideoActivity.MediaItem.MediaType.VIDEO -> loadVideo(holder.playerView, item.url)
//            else -> {}
//        }
    }

    override fun getItemCount() = mediaList.size

    inner class MediaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.image_view)
        val videoView: VideoView = itemView.findViewById(R.id.videoView)
//        val playerView: PlayerVieww = itemView.findViewById(R.id.player_view)
    }

    // Glide 加载图片
    private fun loadImage(imageView: ImageView, url: String) {
        Glide.with(context)
            .load(url)
            .placeholder(R.mipmap.image_zanwei)
            .error(R.mipmap.image_zanwei)
            .into(imageView)
    }

    // ExoPlayer 加载视频
//    private fun loadVideo(playerView: PlayerView, url: String) {
//        val mediaItem = MediaItem.fromUri(Uri.parse(url))
//        val player = ExoPlayerFactory.newSimpleInstance(context)
//        player.setMediaItem(mediaItem)
//        player.prepare()
//        playerView.player = player
//
//        // 绑定播放状态监听
//        player.addListener(object : Player.Listener() {
//            override fun onPlaybackStateChanged(state: PlaybackState) {
//                updatePlayButtonState(player, state)
//            }
//        })
//    }
//
//    private fun updatePlayButtonState(player: Player, state: PlaybackState) {
//        when (state) {
//            PlaybackState.PLAYING -> showPlayControls(playerView)
//            PlaybackState.PAUSED, PlaybackState.IDLE -> hidePlayControls(playerView)
//        }
//    }
//
//    private fun showPlayControls(playerView: PlayerView) {
//        playerView.findViewById<Button>(R.id.play_button).visibility = View.VISIBLE
//        playerView.findViewById<Button>(R.id.pause_button).visibility = View.GONE
//    }
//
//    private fun hidePlayControls(playerView: PlayerView) {
//        playerView.findViewById<Button>(R.id.play_button).visibility = View.GONE
//        playerView.findViewById<Button>(R.id.pause_button).visibility = View.VISIBLE
//    }


}